<!--
 * @Date         : 2024-03-01 20:29:20
 * @LastEditTime : 2025年11月25 14:59:25
 * @description  : 
-->
<script setup>
const isyydt = import.meta.env.VITE_API_DEV_ENV == "yydt";
import router from "@/router";
import TheWelcome from "../components/TheWelcome.vue";
import { getExamInfo, getUserInfo, login, getExamProblem } from "@/api/";
import { clearAllSession } from "@/utils/auth";
const ruleFormRef = ref();
const endTimeintervalId = ref("");
const form = reactive({
  key: "b1e11b00-c54d-4156-bd0c-ddcb11d0d4dc",
  examinee_number: "",
  idcard: "",
  // examinee_number: "233293023",
  // idcard: "410258198712042021",
});
const rules = reactive({
  examinee_number: [
    { required: true, message: "请输入准考证号", trigger: "blur" },
  ],
  idcard: [{ required: true, message: "请输入身份证号", trigger: "blur" }],
});
// 登录
const onSubmit = async (formEl) => {
  if (!formEl) return;
  console.log(formEl);
  await formEl.validate((valid, fields) => {
    if (valid) {
      clearAllSession();
      sessionStorage.setItem("ROLES", "entrant");
      login(form).then((res) => {
        sessionStorage.setItem("api-token", res.data.token);
        getUserInfo().then((res1) => {
          sessionStorage.setItem("userInfo", JSON.stringify(res1.data));
          getExamInfo().then((res2) => {
            sessionStorage.setItem("examInfo", JSON.stringify(res2.data));
            onStart(res2.data);
            // router.replace("/notice");
          });
        });
      });
    } else {
      console.log("error submit!", fields);
    }
  });
};
onMounted(() => {
  clearAllSession();
  sessionStorage.setItem("ROLES", "entrant");
});
// 开始考试
const onStart = async ({ start_time, end_time }) => {
  const newTime = new Date().getTime();
  start_time = new Date(start_time).getTime();
  end_time = new Date(end_time).getTime();
  // 判断是否到考试时间
  if (newTime < start_time) {
    clearAllSession();
    ElMessage.error("未到考试时间！！");
    return;
  }
  if (newTime > end_time) {
    clearAllSession();
    ElMessage.error("已过考试时间！！");
    return;
  }
  let { id } = JSON.parse(sessionStorage.getItem("examInfo")) || {};
  try {
    let res = await getExamProblem({ id });
    let { problem, exam_info } = res.data;
    if (!problem || JSON.stringify(problem) == "{}") {
      ElMessage({
        showClose: true,
        message: "当前试卷没有题目,请联系老师",
        type: "error",
      });
      return;
    }
  } catch (error) {
    if (error.code != 200) {
      return;
    }
  }
  // clearInterval(endTimeintervalId.value);
  // isProgress.value = true;
  sessionStorage.setItem("startTime", +new Date());
  sessionStorage.setItem("endTime", end_time);
  // let { minute } = JSON.parse(sessionStorage.getItem("examInfo")) || {};
  // let endTime = +new Date() + Number(minute) * 60 * 1000;
  // sessionStorage.setItem("endTime", end_time);
  // endTimeFun(end_time);
  isyydt
    ? router.replace("/answerPage_yydt")
    : router.replace("/answerPage_zcydt");
};
// 是否是练习
const isShow = () => {
  let hostname = window.location.hostname;
  let protocol = window.location.protocol + "//";
  let url = protocol + hostname;
  if (
    url == "https://yydtmoni.yfzxzjysg.top" ||
    url == "https://zcydtmoni.yfzxzjysg.top"
  ) {
    return true;
  }
  return false;
  // return location.href.includes("zydt");
};
</script>

<template>
  <div class="about">
    <div class="from">
      <div class="title">
        {{ isyydt ? "中药处方审核考试" : "中成药介绍考试" }}
      </div>
      <el-form
        :model="form"
        label-width="auto"
        :rules="rules"
        ref="ruleFormRef"
        label-position="right"
        :hide-required-asterisk="true"
      >
        <el-form-item label="准考证号:" prop="examinee_number">
          <el-input
            style="width: 227px"
            v-model="form.examinee_number"
            placeholder="请输入准考证号"
          />
        </el-form-item>
        <el-form-item label="身份证号:" prop="idcard">
          <el-input
            style="width: 227px"
            v-model="form.idcard"
            type="password"
            placeholder="请输入身份证号"
          />
        </el-form-item>
      </el-form>
      <div class="onSubmit">
        <el-button
          type="primary"
          color="#652E1B"
          style="width: 180px; height: 39px"
          @click="onSubmit(ruleFormRef)"
        >
          开始考试
        </el-button>
      </div>
    </div>
  </div>
</template>
<style lang="scss">
.about {
  min-width: 100vw;
  height: 100%;
  background-image: url("@/assets/background.png");
  background-repeat: no-repeat;
  background-size: 100% 100%;
  display: flex;
  justify-content: center;
  align-items: center;
  .from {
    width: 551px;
    height: 370px;
    background-image: url("@/assets/login.png");
    background-repeat: no-repeat;
    background-size: 100% 100%;
    box-sizing: border-box;
    display: flex;
    flex-direction: column;
    justify-content: center;
    padding: 37px;
    align-items: center;
    transform: scale(1.5);
    .el-form-item__label {
      color: #333 !important;
      font-size: 18px;
      font-weight: 600;
    }
    .el-button > span {
      letter-spacing: 4px;
      font-weight: 600;
      font-size: 18px;
    }
    .title {
      color: #970702;
      font-size: 37px;
      text-align: center;
      padding: 37px 0;
      font-weight: 400;
    }
    .onSubmit {
      margin-top: 23px;
      width: 100%;
      height: 100%;
      display: flex;
      justify-content: center;
      align-items: center;
    }
  }
}
</style>
